import axios from 'axios'
import { ElMessage } from 'element-plus'
const http = axios.create({
  baseURL: `/api`,
})

http.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

http.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      return Promise.reject(response)
    } else if (response.data instanceof Blob) {
      return response.data
    } else if (response.config.headers.res) {
      return response
    }
    return response.data.data
  },
  (error) => {
    if (error.response && error.response.status === 401) {
      ElMessage({
        message: '系统错误',
        type: 'error',
        grouping: true,
      })
    } else {
      return Promise.reject(error)
    }
  },
)

export default http
